#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 1e6 + 10, INF = 0x3f3f3f3f;

ll at(ll A, ll B, ll C, ll D) { return (A << 24) | (B << 16) | (C << 8) | D; }

inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch>'9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

ll mark(ll x)
{
    if (x <= 0) return 0;
    return ((1ll << 8) - 1) - ((1ll << (8 - x)) - 1);
}

void solve()
{
    ll A, B, C, D, E, n;
    A = read();B = read();C = read(); D = read(); E = read();
    n = read();

    // cout << A << " ";
    // cout << B << " ";
    // cout << C << " ";
    // cout << D << " ";

    ll m1 = mark(min(8ll, E)), m2 = mark(min(8ll, E - 8));
    ll m3 = mark(min(8ll, E - 16)), m4 = mark(min(8ll, E - 24));

    ll a, b, c, d;
    for (ll i = 1;i <= n;i++) {
        a = read();b = read();c = read();d = read();

        if ((a & m1) == (A & m1) && (b & m2) == (B & m2) && (c & m3) == (C & m3) && (d & m4) == (D & m4)) cout << "YES\n";
        else cout << "NO\n";
    }
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    //cin>>T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}